#!/bin/bash

# 检查参数
if [ -z "$1" ]; then
    echo "错误：请提供版本号参数"
    echo "用法：./runterminal.sh version"
    exit 1
fi

VERSION=$1
IMAGE_NAME="mydockerregistry.tommickey.cn/tommickey/terminal"
CONTAINER_NAME="terminal"
CONFIG_PATH="/docker/terminal/config.yaml"

# 检查配置文件是否存在
if [ ! -f "$CONFIG_PATH" ]; then
    echo "错误：配置文件不存在: $CONFIG_PATH"
    exit 1
fi

# 停止并删除已存在的容器
echo "停止并删除已存在的容器..."
docker stop $CONTAINER_NAME 2>/dev/null
docker rm $CONTAINER_NAME 2>/dev/null

# 启动新容器
echo "启动新容器..."
docker run -d \
    --name $CONTAINER_NAME \
    --restart always \
    --network host \
    -v $CONFIG_PATH:/app/config.yaml \
    -e TZ=Asia/Shanghai \
    -e DOCKER_CONTAINER=true \
    $IMAGE_NAME:$VERSION

# 检查容器是否成功启动
if [ $? -eq 0 ]; then
    echo "容器启动成功！"
    echo "容器名称: $CONTAINER_NAME"
    echo "镜像版本: $VERSION"
    echo "配置文件: $CONFIG_PATH"
    echo "网络模式: host"
    
    # 显示容器日志
    echo "容器日志:"
    docker logs $CONTAINER_NAME
else
    echo "错误：容器启动失败"
    exit 1
fi 